/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package merlion.mrp.ppm.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;

/**
 *
 * @author Zhu Xin, Zhang Ying
 */
@Entity
public class IndivSOP implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    @OrderBy("index")
    //@OneToMany(targetEntity=IndexNValue.class, cascade={CascadeType.ALL}, fetch= FetchType.EAGER)
    private List<IndexNValue> salesforcastValues = new ArrayList<IndexNValue>(); // size of 3
    
    @OrderBy("index")
    //@OneToMany(targetEntity=IndexNValue.class, cascade={CascadeType.ALL}, fetch= FetchType.EAGER)
    private List<IndexNValue> unplannedDemandList = new ArrayList<IndexNValue>();  //length of the list should be 3
    
    @OrderBy("index")
    //@OneToMany(targetEntity=IndexNValue.class, cascade={CascadeType.ALL}, fetch= FetchType.EAGER)
    private List<IndexNValue> productionPlanList = new ArrayList<IndexNValue>();
    
    @OrderBy("index")
    //@OneToMany(targetEntity=IndexNValue.class, cascade={CascadeType.ALL}, fetch= FetchType.EAGER)
    private List<IndexNValue> inventoryList = new ArrayList<IndexNValue>();
    
    // to indicate which product, 0- A, 1-B, 2-C, 3-D
    private int productIndex;
    
    @ManyToOne(targetEntity=Product.class, cascade={CascadeType.MERGE},fetch=FetchType.EAGER)
    private Product product;
    
    @OrderBy("archiveIndex")
    @OneToMany(targetEntity = ArchivedIndivSOP.class, cascade = {CascadeType.MERGE}, fetch = FetchType.EAGER)
    private Collection<ArchivedIndivSOP> archivedIndivSops = new ArrayList<ArchivedIndivSOP>();

    public IndivSOP() {
        for (int i = 0; i < 3; i++) {
            IndexNValue temp1 = new IndexNValue(i, new Long(0));
            IndexNValue temp2 = new IndexNValue(i, new Long(0));
            IndexNValue temp3 = new IndexNValue(i, new Long(0));
            IndexNValue temp4 = new IndexNValue(i, new Long(0));
            unplannedDemandList.add(temp1);
            productionPlanList.add(temp2);
            inventoryList.add(temp3);
            salesforcastValues.add(temp4);
        }
    }

    public ArchivedIndivSOP returnUpdateIndivs(int i) {
        if (archivedIndivSops == null || archivedIndivSops.isEmpty()) {
            return null;
        }
        List<ArchivedIndivSOP> temp = (List<ArchivedIndivSOP>) archivedIndivSops;
        return temp.get(i);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public List<IndexNValue> getInventoryList() {
        return inventoryList;
    }

    public void setInventoryList(List<IndexNValue> inventoryList) {
        this.inventoryList = inventoryList;
    }

    public int getProductIndex() {
        return productIndex;
    }

    public void setProductIndex(int productIndex) {
        this.productIndex = productIndex;
    }

    public List<IndexNValue> getProductionPlanList() {
        return productionPlanList;
    }

    public void setProductionPlanList(List<IndexNValue> productionPlanList) {
        this.productionPlanList = productionPlanList;
    }

    public List<IndexNValue> getSalesforcastValues() {
        return salesforcastValues;
    }

    public void setSalesforcastValues(List<IndexNValue> salesforcastValues) {
        this.salesforcastValues = salesforcastValues;
    }

    public List<IndexNValue> getUnplannedDemandList() {
        return unplannedDemandList;
    }

    public void setUnplannedDemandList(List<IndexNValue> unplannedDemandList) {
        this.unplannedDemandList = unplannedDemandList;
    }

    public Collection<ArchivedIndivSOP> getArchivedIndivSops() {
        return archivedIndivSops;
    }

    public void setArchivedIndivSops(Collection<ArchivedIndivSOP> archivedIndivSops) {
        this.archivedIndivSops = archivedIndivSops;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {

        if (!(object instanceof IndivSOP)) {
            return false;
        }
        IndivSOP other = (IndivSOP) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "merlion.mrp.ppm.entity.IndivSOP[ id=" + id + " ]";
    }
}
